package com.demon.androidbasic.action

import android.graphics.drawable.Drawable
import android.net.ConnectivityManager
import android.view.View
import androidx.annotation.DrawableRes
import androidx.annotation.RawRes
import androidx.annotation.StringRes
import androidx.core.content.ContextCompat
import com.demon.androidbasic.R
import com.demon.androidbasic.widget.HintLayout

/**
 * author : Android 轮子哥
 * github : https://github.com/getActivity/AndroidProject
 * time   : 2019/12/08
 * desc   : 界面状态提示
 */
interface StatusAction {
    /**
     * 获取提示布局
     */
    val hintLayout: HintLayout

    /**
     * 显示加载中
     */
    fun showLoading(@RawRes id: Int = R.raw.loading) {
        val layout = hintLayout
        layout.show()
        layout.setAnim(id)
        layout.setHint("")
        layout.setOnClickListener(null)
    }

    /**
     * 显示加载完成
     */
    fun showComplete() {
        val layout = hintLayout
        if (layout != null && layout.isShow) {
            layout.hide()
        }
    }

    /**
     * 显示空提示
     */
    fun showEmpty() {
        showLayout(R.drawable.hint_empty_ic, R.string.hint_layout_no_data, null)
    }

    /**
     * 显示错误提示
     */
    fun showError(listener: View.OnClickListener?) {
        val layout = hintLayout
        val context = layout.context
        val manager = ContextCompat.getSystemService(context, ConnectivityManager::class.java)
        if (manager != null) {
            val info = manager.activeNetworkInfo
            // 判断网络是否连接
            if (info == null || !info.isConnected) {
                showLayout(R.drawable.hint_network_ic, R.string.hint_layout_error_network, listener)
                return
            }
        }
        showLayout(R.drawable.hint_error_ic, R.string.hint_layout_error_request, listener)
    }

    /**
     * 显示自定义提示
     */
    fun showLayout(
        @DrawableRes drawableId: Int,
        @StringRes stringId: Int,
        listener: View.OnClickListener?
    ) {
        val layout = hintLayout
        val context = layout.context
        showLayout(
            ContextCompat.getDrawable(context, drawableId),
            context.getString(stringId),
            listener
        )
    }

    fun showLayout(drawable: Drawable?, hint: CharSequence?, listener: View.OnClickListener?) {
        val layout = hintLayout
        layout.show()
        layout.setIcon(drawable)
        layout.setHint(hint)
        layout.setOnClickListener(listener)
    }
}